package com.rabbiter.fm.controller;

import com.rabbiter.fm.common.enums.ErrorMsg;
import com.rabbiter.fm.model.ArticleModel;
import com.rabbiter.fm.model.IdleItemModel;
import com.rabbiter.fm.service.ArticleItemService;
import com.rabbiter.fm.service.IdleItemService;
import com.rabbiter.fm.vo.ResultVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("article")
public class ArticleItemController {

    @Autowired
    private ArticleItemService articleItemService;

    @PostMapping("add")
    public ResultVo addIdleItem(@CookieValue("shUserId")
                                @NotNull(message = "登录异常 请重新登录")
                                @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                                @RequestBody ArticleModel idleItemModel) {
        idleItemModel.setCreateUserId(Long.valueOf(123));
        idleItemModel.setCreateTime(new Date());
        idleItemModel.setCreateUserName("普通用户");
        System.out.println("猴哥添加前的数据源" + idleItemModel.toString());
        if (articleItemService.addArticleItem(idleItemModel)) {
            System.out.println("猴哥新建返回的数据源" + idleItemModel.toString());
            return ResultVo.success(idleItemModel);
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }


    @GetMapping("find")
    public ResultVo findIdleItem(@RequestParam(value = "findValue", required = false) String findValue,
                                 @RequestParam(value = "page", required = false) Integer page,
                                 @RequestParam(value = "nums", required = false) Integer nums) {
        if (null == findValue) {
            findValue = "";
        }
        System.out.println("文章列表接受参数page="+page+"nums="+nums);
        int p = 1;
        int n = 8;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return ResultVo.success(articleItemService.findArticItem(findValue, p, n));
    }


    @GetMapping("info")
    public ResultVo getArticleItem(@RequestParam Long id){
        return ResultVo.success(articleItemService.getArticleItem(id));
    }


    @GetMapping("detale")
    public ResultVo getArticleItem2(@RequestParam Long id){
//        return ResultVo.success(articleItemService.getArticleItem(id));
        System.out.println("删除文22章"+id);
        if (articleItemService.deleteArticle(id)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }


    @PostMapping("like")
    public ResultVo getDianZan(@Param("id") Long id){
        System.out.println("点赞章"+id);
        if (articleItemService.updateDianZan(id)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

}
